import 'package:flustars_flutter3/flustars_flutter3.dart';
import 'package:flutter/material.dart';
import 'package:flutter_taotaoshipin/common/theme/app_colors.dart';
import 'package:flutter_taotaoshipin/model/goods_list_entity.dart';
import 'package:flutter_taotaoshipin/util/image_utils.dart';
import 'package:flutter_taotaoshipin/widget/load_image.dart';

class SubscriptionItem extends StatefulWidget {
  const SubscriptionItem({
    super.key,
    required this.entity,
    this.onTap,
    this.isSelected = false,
    this.isSubscripted = false,
  });

  final GoodsListEntity entity;
  final VoidCallback? onTap;
  final bool isSelected;
  final bool isSubscripted;

  @override
  State<SubscriptionItem> createState() => _SubscriptionItemState();
}

class _SubscriptionItemState extends State<SubscriptionItem> {
  String bottom1 = '';
  String bottom2 = '';

  @override
  void initState() {
    // widget.entity.bottomText = '¥50_立省7元';
    var list = widget.entity.bottomText!.split('_');
    if (list.isNotEmpty) {
      bottom1 = list[0];
      if (list.length > 1) {
        bottom2 = list[1];
      }
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    var textColor = widget.isSelected ? Colors.white : AppColors.text;
    return InkWell(
      onTap: widget.onTap,
      child: Stack(
        clipBehavior: Clip.none,
        children: [
          Container(
            width: double.infinity,
            height: double.infinity,
            padding: EdgeInsets.symmetric(vertical: 12),
            decoration: BoxDecoration(
              image: widget.isSelected
                  ? DecorationImage(
                      image:
                          ImageUtils.getAssetImage('subscription/item_bg_sel'))
                  : null,
              color: widget.isSelected ? AppColors.app_main : Color(0xFFECEDF1),
              borderRadius: BorderRadius.circular(10),
            ),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  widget.entity.name!,
                  style: TextStyle(
                    color: textColor,
                    fontSize: 16,
                  ),
                ),
                RichText(
                  text: TextSpan(
                    text: '¥',
                    style: TextStyle(
                      color: textColor,
                      fontSize: 12,
                    ),
                    children: [
                      TextSpan(
                        text: MoneyUtil.changeYWithUnit(
                          widget.isSubscripted
                              ? widget.entity.subAmount!
                              : widget.entity.amount!,
                          MoneyUnit.NORMAL,
                          format: MoneyFormat.END_INTEGER,
                        ),
                        style: TextStyle(
                          fontSize: 40,
                          fontWeight: FontWeight.w900,
                        ),
                      ),
                    ],
                  ),
                ),
                Text(
                  widget.isSelected ? bottom2 : bottom1,
                  style: TextStyle(
                    color: textColor,
                    fontSize: 12,
                    decoration:
                        widget.isSelected ? null : TextDecoration.lineThrough,
                  ),
                ),
              ],
            ),
          ),
          if (widget.entity.subscript!.isNotEmpty)
            Positioned(
              top: -10,
              left: 0,
              child: LoadImage(
                widget.entity.subscript!,
                height: 20,
              ),
            ),
        ],
      ),
    );
  }
}
